<?php
class Admin_TournamentController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    	$this->_helper->acl->allow('user');
    }
    public function indexAction()
    {
    	$message = $this->_helper->FlashMessenger->getMessages();
	    if($message){
	    	$this->view->message = $message[0];
	    }
		$this->view->title = 'View all tournaments';
   	   	$t = new Application_Model_Tournament();
	   	$request = $this->getRequest();
	   	$this->view->t = $t->getAll(false, null, false);
    }
	public function editAction(){
		$this->view->title = 'Edit tournament';
		$this->view->headTitle($this->view->title, 'PREPEND');
		if($input = $this->_checkInput('id', 'tournament')){
			$t = new Application_Model_Tournament($input->id);
			$form = new Application_Form_Tournament('edit');
			$form->excludeFrom('title', 'Db_NoRecordExists', 'title', $t->title);
			$form->populate($t->getRow());
	    	$request = $this->getRequest();
	    	if($request->isPost()){
    	    	if($form->isValid($request->getPost())){
    	    		$old_name = $t->logo;
    	    		$t->fill($form->getValues());
    	    		//edit logo file
    	    	    if($form->logo->isUploaded()){
	    	   			$cfg = $this->getInvokeArg('bootstrap')->getOption('uploads');
	    	   			$adapter = $form->logo->getTransferAdapter();
	    	   			$fileinfo = $adapter->getFileInfo();
	    	   			$ext = @pathinfo($adapter->getFileName(), PATHINFO_EXTENSION);
	    	   			$generated_name = $form->getValue('title') . '.' . $ext;
	    	   			if($generated_name != $old_name){
	    	   				if(is_file($cfg['uploadPath'] . '/' . $old_name)){
	    	   					unlink($cfg['uploadPath'] . '/' . $old_name);
	    	   				}
	    	   			}
	    	   			$new_name =  $cfg['uploadPath'] . '/' . $generated_name;
						$adapter->addFilter('Rename', array('target' => $new_name, 'overwrite' => 'true'));
						//$adapter->addFilter('Rename', $new_name);
		        		if($adapter->receive()) $t->logo = $generated_name;
    	   			}
    	   			else{
    	   				$t->logo = $old_name;
    	   			}
    	    		$t->save();
    	    		$messenger = $this->_helper->FlashMessenger;
					$messenger->addMessage('Tournament edited');
					$this->_helper->redirector(null, 'tournament', 'admin');
    	    	}    		    		
   			}
   			$this->view->image = $t->logo;
   			$this->view->form = $form;							
	    }
	    else{
	    	throw new Zend_Controller_Action_Exception('Invalid input');
	    }	
	}
	public function addAction(){
		$this->view->title = 'Add tournament';
		$this->view->headTitle($this->view->title, 'PREPEND');
		$form = new Application_Form_Tournament();
		$request = $this->getRequest();
	    if($request->isPost()){
    	   	if($form->isValid($request->getPost())){
    	   		$t = new Application_Model_Tournament();
    	   		$t->fill($form->getValues());
    	   		//save logo file
    	   		if($form->logo->isUploaded()){
    	   			$cfg = $this->getInvokeArg('bootstrap')->getOption('uploads');
    	   			$adapter = $form->logo->getTransferAdapter();
    	   			$fileinfo = $adapter->getFileInfo();
    	   			$ext = @pathinfo($adapter->getFileName(), PATHINFO_EXTENSION);
    	   			$new_name =  $cfg['uploadPath'] . '/' . $form->getValue('title') . '.' . $ext;
					$adapter->addFilter('Rename', array('target' => $new_name, 'overwrite' => 'true'));
					//$adapter->addFilter('Rename', $new_name);
	        		if($adapter->receive()) $t->logo = $form->getValue('title') . '.' . $ext;
    	   		}
    	   		$t->save();
    	   		$messenger = $this->_helper->FlashMessenger;
				$messenger->addMessage('Tournament added');
				$this->_helper->redirector(null, 'tournament', 'admin');
    	   	}
	    }
	    $this->view->form = $form;		
	}
	public function deleteAction(){
		$this->_helper->viewRenderer->setNoRender();
		if($input = $this->_checkInput('id', 'tournament')){
			$t = new Application_Model_Tournament($input->id);
			$t->delete();
			$messenger = $this->_helper->FlashMessenger;
			$messenger->addMessage('Tournament deleted');
			$this->_helper->redirector(null, 'tournament', 'admin');
		}
		else{
			throw new Zend_Controller_Action_Exception('Invalid input');
		}		
	}
	private function _checkInput($param, $field = null){
		$validatorsSet = array();
		$validatorsSet[] = 'Int';
		$validatorsSet[] = new Zend_Validate_GreaterThan(0);
		$validatorsSet[] = new Zend_Validate_NotEmpty(Zend_Validate_NotEmpty::ALL);
		if($field){
			$validatorsSet[] = new Zend_Validate_Db_RecordExists(array('table' => $field, 'field' => $param));
		}
    	$validators = array($param => $validatorsSet);
	    $input = new Zend_Filter_Input(null, $validators);
	    $input->setData($this->getRequest()->getParams());
	    if($input->isValid()){
	    	return $input;
	    }
	    else{
	    	return false;
	    }		
	}
}